package com.ruoyi.web.controller.repair;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.company.domain.HouseCompanyT;
import com.ruoyi.company.service.IHouseCompanyTService;
import com.ruoyi.data.DataUtils;
import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.project.domain.ParameterSingleVO;
import com.ruoyi.project.service.IHouseProjectTService;
import com.ruoyi.projectInfo.domain.HouseProjectInfo;
import com.ruoyi.projectInfo.service.IHouseProjectInfoService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 维修项目Controller
 *
 * @author tuyeqi
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/repair/project")
public class HouseProjectTController extends BaseController {
    @Autowired
    private IHouseProjectTService houseProjectTService;
    private List<HouseProjectT> houseProjectList = new ArrayList<HouseProjectT>();
    private List<HouseProjectT> houseProjectListTemp = new ArrayList<HouseProjectT>();
    @Autowired
    private IHouseCompanyTService houseCompanyTService;
    @Autowired
    private IHouseProjectInfoService houseProjectInfoService;
    private ParameterSingleVO parameterSingleVO = new ParameterSingleVO();
    private String generateFileName;
    private String targetFileName;
    private Integer userNumber = 0;
    private XWPFDocument document = new XWPFDocument();
    private HttpServletResponse response;
    private String filePath;

    /**
     * 查询维修项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseProjectT houseProjectT) {
        startPage();
        List<HouseProjectT> list = houseProjectTService.selectHouseProjectTList(houseProjectT);
        for(HouseProjectT houseProjectVO : list){
            houseProjectVO.setSignSts("1");
            HouseProjectT houseProjectT1 = new HouseProjectT();
            houseProjectT1 = houseProjectTService.selectRepairBySignSts(houseProjectVO);
            HouseProjectT houseProjectT2 = new HouseProjectT();
            houseProjectVO.setSignSts("");
            houseProjectT2 = houseProjectTService.selectRepairBySignSts(houseProjectVO);
            if(houseProjectT2.getSignSum()==0){
                houseProjectVO.setSignSum(0d);
            }else{
                houseProjectVO.setSignSum(DataUtils.divideMoney(houseProjectT1.getSignSum(),houseProjectT2.getSignSum()));
            }
        }
        if(getDataTableSumJudge(list)){
            HouseProjectT houseProjectVO = houseProjectTService.selectHouseProjectSum(houseProjectT);
            houseProjectVO.setProjectId("合计");
            list.add(houseProjectVO);
        }
        return getDataTable(list);
    }

    /**
     * 导出维修项目列表
     */
    @Log(title = "维修项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseProjectT houseProjectT) {
        List<HouseProjectT> list = houseProjectTService.selectHouseProjectTList(houseProjectT);
        if(getDataTableSumJudge(list)){
            HouseProjectT houseProjectVO = houseProjectTService.selectHouseProjectSum(houseProjectT);
            houseProjectVO.setProjectId("合计");
            list.add(houseProjectVO);
        }
        ExcelUtil<HouseProjectT> util = new ExcelUtil<HouseProjectT>(HouseProjectT.class);
        util.exportExcel(response, list, "维修项目数据");
    }

    /**
     * 获取维修项目详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(houseProjectTService.selectHouseProjectTById(id));
    }

    /**
     * 新增维修项目
     */
    @Log(title = "维修项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseProjectT houseProjectT) {
        houseProjectT.setCreater(getUsername());
        return toAjax(houseProjectTService.insertHouseProjectT(houseProjectT));
    }

    /**
     * 修改维修项目
     */
    @PostMapping(value = "/updateProject")
    public AjaxResult updateProject(@RequestBody HouseProjectT houseProjectT) {
        HouseProjectInfo infoVO=new HouseProjectInfo();
        infoVO.setInfoName(houseProjectT.getInfoName());
        infoVO=houseProjectInfoService.selectInfoName(infoVO);
        HouseCompanyT companyVO=new HouseCompanyT();
        companyVO.setCompanyName(houseProjectT.getCompanyName());
        companyVO=houseCompanyTService.selectCompanyName(companyVO);
        HouseProjectT projectVO=new HouseProjectT();
        houseProjectT.setInfoId(infoVO.getInfoId());
        houseProjectT.setCompanyId(companyVO.getCompanyId());
        return toAjax(houseProjectTService.updateHouseProjectT(houseProjectT));
    }

    /**
     * 删除维修项目
     */
    @Log(title = "维修项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (long id : ids) {
            HouseProjectT houseProjectVO = new HouseProjectT();
            houseProjectVO = houseProjectTService.selectHouseProjectTById(id);
            if ("1".equals(houseProjectVO.getProjectSts())) {
                return AjaxResult.error("该工程已执行完毕,不允许删除！");
            }
        }
        return toAjax(houseProjectTService.deleteHouseProjectTByIds(ids));
    }

    /**
     * 执行
     */
    @GetMapping(value = "/executeProject/{id}/{executeDate}")
    public AjaxResult getExecuteProject(@PathVariable("id") Long id,@PathVariable("executeDate") String executeDate) {
        HouseProjectT houseProjectVO = new HouseProjectT();
        houseProjectVO = houseProjectTService.selectHouseProjectTById(id);
        if (houseProjectVO.getPayMoney() == null) {
            return AjaxResult.error("该工程未核算！");
        }
        if ("1".equals(houseProjectVO.getProjectSts())) {
            return AjaxResult.error("该工程已执行完毕！");
        }
        houseProjectVO.setExecuter(getUsername());
        houseProjectVO.setExecuteDate( DateUtils.dateTime("yyyy-MM-dd",executeDate));
        List<HouseProjectT> houseProjectList = houseProjectTService.getProjectUser(houseProjectVO);
        for (HouseProjectT projectVO : houseProjectList) {
            houseProjectTService.updateRepairMoney(projectVO);
            houseProjectVO.setProjectId(houseProjectVO.getProjectId());
            houseProjectVO.setExecuteDate(houseProjectVO.getExecuteDate());
            houseProjectTService.updateRepairDate(houseProjectVO);
        }
        houseProjectTService.updateSts(houseProjectVO);
        return toAjax(1);
    }

    /*撤销*/
    @GetMapping(value = "/revokeProject/{id}")
    @Transactional
    public AjaxResult getRevokeProject(@PathVariable("id")Long id){
        HouseProjectT houseProjectVO = new HouseProjectT();
        houseProjectVO = houseProjectTService.selectHouseProjectTById(id);
        if(houseProjectVO.getExecuteDate()!=null) {
            Date day = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            String strDate2 = sdf2.format(houseProjectVO.getExecuteDate());
            String strDate1 = sdf.format(day);
            if (strDate1 == strDate2) {
                return AjaxResult.error("非当日执行扣款，不能撤销！");
            }
        }
        if ("0".equals(houseProjectVO.getProjectSts())) {
            return AjaxResult.error("该工程未执行,可以直接进行删除项目！");
        }
        houseProjectVO.setExecuter(getUsername());
        houseProjectVO.setProjectId(houseProjectVO.getProjectId());
        List<HouseProjectT> houseProjectList = houseProjectTService.getProjectUser(houseProjectVO);
        for (HouseProjectT projectVO : houseProjectList) {
            houseProjectTService.updateRepairRevoke(projectVO);
        }
        houseProjectTService.deleteRepairForRefund(houseProjectVO);
        houseProjectTService.deleteHouseProjectTById(id);
        return toAjax(1);

    }

    /**
     * 获取随机码
     *
     * @return fourRandom
     */
    public String getFourRandom() {
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++) {
                fourRandom = "0" + fourRandom;
            }
        }
        return fourRandom;
    }
}
